package com.eyedog.video.http;

import android.text.TextUtils;
import android.util.Log;

import com.eyedog.library.utils.MD5Util;
import com.eyedog.video.constant.Constants;
import com.eyedog.video.entity.CategoryEntity;
import com.eyedog.video.entity.HotTagEntity;
import com.eyedog.video.entity.RespEntity;
import com.eyedog.video.entity.VideoEntity;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * created by jw200 at 2019/5/17 12:38
 **/
public class HttpClientVideo {
    private final String TAG = "HttpClient";

    enum Instance {
        INSTANCE;

        HttpClientVideo client;

        Instance() {
            client = new HttpClientVideo();
        }
    }

    public static HttpClientVideo getInstance() {
        return HttpClientVideo.Instance.INSTANCE.client;
    }

    private String baseUrl;

    private Retrofit retrofit;

    private HttpClientVideo() {
    }

    public void init(String baseurl) {
        if (!TextUtils.equals(baseUrl, baseurl) || retrofit == null) {
            this.baseUrl = baseurl;
            retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create())
                .baseUrl(baseurl).build();
        }
    }

    private String signString(String timeStamp) {
        return MD5Util.MD5(timeStamp + Constants.appID + Constants.secretKey);
    }

    private String getTimeStamp() {
        return System.currentTimeMillis() + "";
    }

    public void requestCategories(final ICallback<List<CategoryEntity>> callback) {
        if (HttpClient.getInstance().hasApiRequested()) {
            ICategoryService categoryService = retrofit.create(ICategoryService.class);
            String time = getTimeStamp();
            categoryService.getCategoryList(time, Constants.appID, signString(time))
                .enqueue(new Callback<RespEntity<List<CategoryEntity>>>() {
                    @Override
                    public void onResponse(Call<RespEntity<List<CategoryEntity>>> call,
                        Response<RespEntity<List<CategoryEntity>>> response) {
                        if (response.isSuccessful() && response.body() != null) {
                            callback.onLoadSuccess(response.body().data);
                        } else {
                            callback.onLoadFailed("response not success");
                        }
                    }

                    @Override
                    public void onFailure(Call<RespEntity<List<CategoryEntity>>> call,
                        Throwable t) {
                        callback.onLoadFailed("response onFailure " + t.getMessage());
                    }
                });
        } else {
            //
        }
    }

    public void requestRecommendVideos(int page, final ICallback<List<VideoEntity>> callback) {
        Log.i(TAG, "requestRecommendVideos " + HttpClient.getInstance().hasApiRequested());
        if (HttpClient.getInstance().hasApiRequested()) {
            IRecommendVideoService recommedService = retrofit.create(IRecommendVideoService.class);
            String time = getTimeStamp();
            String app_id = Constants.appID + "";
            String sign = signString(time);
            recommedService.getRecommendList(time, app_id, sign, page)
                .enqueue(new Callback<RespEntity<List<VideoEntity>>>() {
                    @Override
                    public void onResponse(Call<RespEntity<List<VideoEntity>>> call,
                        Response<RespEntity<List<VideoEntity>>> response) {
                        Log.i(TAG,
                            "requestRecommendVideos onResponse " + response.isSuccessful());
                        if (response.isSuccessful() && response.body() != null) {
                            callback.onLoadSuccess(response.body().data);
                        } else {
                            callback.onLoadFailed("response not success");
                        }
                    }

                    @Override
                    public void onFailure(Call<RespEntity<List<VideoEntity>>> call,
                        Throwable t) {
                        Log.i(TAG, "requestRecommendVideos onFailure " + t.getMessage());
                        callback.onLoadFailed("response onFailure " + t.getMessage());
                    }
                });
        } else {
            //
        }
    }

    public void requestNewestVideos(int page, final ICallback<List<VideoEntity>> callback) {
        Log.i(TAG, "requestNewestVideos " + HttpClient.getInstance().hasApiRequested());
        if (HttpClient.getInstance().hasApiRequested()) {
            INewestVideoService newestService = retrofit.create(INewestVideoService.class);
            String time = getTimeStamp();
            String app_id = Constants.appID + "";
            String sign = signString(time);
            newestService.getNewestVideoList(time, app_id, sign, page)
                .enqueue(new Callback<RespEntity<List<VideoEntity>>>() {
                    @Override
                    public void onResponse(Call<RespEntity<List<VideoEntity>>> call,
                        Response<RespEntity<List<VideoEntity>>> response) {
                        if (response.isSuccessful() && response.body() != null) {
                            callback.onLoadSuccess(response.body().data);
                        } else {
                            callback.onLoadFailed("response not success");
                        }
                    }

                    @Override
                    public void onFailure(Call<RespEntity<List<VideoEntity>>> call,
                        Throwable t) {
                        callback.onLoadFailed("response onFailure " + t.getMessage());
                    }
                });
        } else {
            //
        }
    }

    public void requestCategoryVideos(int page, int categoryId,
        final ICallback<List<VideoEntity>> callback) {
        if (HttpClient.getInstance().hasApiRequested()) {
            ICategoryVideoService categoryVideoService = retrofit
                .create(ICategoryVideoService.class);
            String time = getTimeStamp();
            String app_id = Constants.appID + "";
            String sign = signString(time);
            categoryVideoService
                .getCategoryVideoList(time, app_id, sign, page + "", categoryId + "")
                .enqueue(new Callback<RespEntity<List<VideoEntity>>>() {
                    @Override
                    public void onResponse(Call<RespEntity<List<VideoEntity>>> call,
                        Response<RespEntity<List<VideoEntity>>> response) {
                        if (response.isSuccessful() && response.body() != null) {
                            callback.onLoadSuccess(response.body().data);
                        } else {
                            callback.onLoadFailed("response not success");
                        }
                    }

                    @Override
                    public void onFailure(Call<RespEntity<List<VideoEntity>>> call,
                        Throwable t) {
                        callback.onLoadFailed("response onFailure " + t.getMessage());
                    }
                });
        } else {
            //
        }
    }

    public void requestVideoList(int page, String keywords,
        final ICallback<List<VideoEntity>> callback) {
        if (HttpClient.getInstance().hasApiRequested()) {
            ISearchService searchService = retrofit.create(ISearchService.class);
            String time = getTimeStamp();
            String appId = Constants.appID;
            String sign = signString(time);
            searchService.getVideoList(time, appId, sign, keywords, page + "")
                .enqueue(new Callback<RespEntity<List<VideoEntity>>>() {
                    @Override
                    public void onResponse(Call<RespEntity<List<VideoEntity>>> call,
                        Response<RespEntity<List<VideoEntity>>> response) {
                        if (response.isSuccessful() && response.body() != null) {
                            callback.onLoadSuccess(response.body().data);
                        } else {
                            callback.onLoadFailed("response not success");
                        }
                    }

                    @Override
                    public void onFailure(Call<RespEntity<List<VideoEntity>>> call,
                        Throwable t) {
                        callback.onLoadFailed("response onFailure " + t.getMessage());
                    }
                });
        } else {
            //
        }
    }

    public void requestHotTagList(final ICallback<List<HotTagEntity>> callback) {
        if (HttpClient.getInstance().hasApiRequested()) {
            IHotTagService tagService = retrofit.create(IHotTagService.class);
            String time = getTimeStamp();
            String appId = Constants.appID;
            String sign = signString(time);
            tagService.getHotTagList(time, appId, sign)
                .enqueue(new Callback<RespEntity<List<HotTagEntity>>>() {
                    @Override
                    public void onResponse(Call<RespEntity<List<HotTagEntity>>> call,
                        Response<RespEntity<List<HotTagEntity>>> response) {
                        if (response.isSuccessful() && response.body() != null) {
                            callback.onLoadSuccess(response.body().data);
                        } else {
                            callback.onLoadFailed("response not success");
                        }
                    }

                    @Override
                    public void onFailure(Call<RespEntity<List<HotTagEntity>>> call,
                        Throwable t) {
                        callback.onLoadFailed("response onFailure " + t.getMessage());
                    }
                });
        } else {
            //
        }
    }

}
